package cn.eonshine.cuba.inf.modular.log.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.eonshine.cuba.inf.common.http.Message;
import cn.eonshine.cuba.inf.modular.log.entity.Log;
import cn.eonshine.cuba.inf.modular.log.entity.LogDetail;
import cn.eonshine.cuba.inf.modular.log.mapper.LogDetailMapper;
import cn.eonshine.cuba.inf.modular.log.mapper.LogMapper;
import cn.eonshine.cuba.inf.modular.log.service.ILogService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2018-08-11
 */
@Service
@Transactional
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
//	@Autowired
//	private ILogDetailService iLogDetailService;
	
	@Autowired
	private LogDetailMapper logDetailMapper;
	
	@Override
	public IPage<Log> searchList(IPage<Log> page, String condition, String logTypeId) {
		QueryWrapper<Log> entity = new QueryWrapper<Log>();
//		Wrapper<Log> entity = new QueryWrapper<Log>();
		if (StringUtils.isNotBlank(condition)) {
			//entity.like("OPERATE_URL", condition);
			entity.lambda().like(Log::getOperateUrl, condition);
		}
		if(StringUtils.isNotBlank(logTypeId)) {
			entity.lambda().eq(Log::getLogTypeId, logTypeId);
		}
//		entity.orderBy("OPERATE_TIME desc, PROCESS_ID ");
		entity.orderBy(true, false, "OPERATE_TIME", "PROCESS_ID");
		return page(page, entity);
	}

	@Override
	public Log selectDetail(String infLogId) {
		QueryWrapper<Log> entity = new QueryWrapper<Log>();
		entity.eq("id", infLogId);
		Log log = getOne(entity);
		QueryWrapper<LogDetail> entityDetail = new QueryWrapper<LogDetail>();
		entityDetail.eq("LOG_ID", infLogId);
		entityDetail.orderByAsc("id");
//		LogDetail logDetail = iLogDetailService.getOne(entityDetail);
		LogDetail logDetail = logDetailMapper.selectOne(entityDetail);
		if (logDetail != null) {
			log.setOpData(logDetail.getOpData());
			if (StringUtils.isNotBlank(logDetail.getException())) {
				log.setException(logDetail.getException());
			}

		}
		return log;
	}

	@Override
	public void deleteByOne(String infLogId) {
		QueryWrapper<LogDetail> entityDetail = new QueryWrapper<LogDetail>();
		entityDetail.eq("LOG_ID", infLogId);
//		iLogDetailService.remove(entityDetail);
		logDetailMapper.delete(entityDetail);
		removeById(infLogId);
	}

	@Override
	public void insertLog(Message message, String uuid, String operateUrl, int count, String exceptions, String logTypeId) {
		Log logs=new Log();
		logs.setProcessId(uuid);
		logs.setOperateUrl(operateUrl);
		logs.setTotal( new BigDecimal(message.getCount()));
		logs.setStatus(String.valueOf(message.getStatus()));
		logs.setSuccessNum(new BigDecimal(count));
		logs.setOperateTime(Calendar.getInstance().getTime());
		logs.setLogTypeId(logTypeId);
		save(logs);
		LogDetail logdetail=new LogDetail();
		logdetail.setOpData(message.toString());
		logdetail.setLogId(logs.getId());
		if(StringUtils.isNotBlank(exceptions)) {
			logdetail.setException(exceptions);
		}
//		iLogDetailService.save(logdetail);
		logDetailMapper.insert(logdetail);
	}

	public IPage<Log> selectCleanLogList(Page<Log> page, Date lastEndDays) {
		return this.baseMapper.selectCleanLogList(page, lastEndDays);
	}
}
